package com.apchernykh.types;

import java.util.ArrayList;
import java.util.List;
import com.apchernykh.rozhlasapp.player.IPlaybackService;
import com.apchernykh.rozhlasapp.player.IPlaybackStateListener;
import com.apchernykh.rozhlasapp.player.NullPlaybackService;
import com.apchernykh.rozhlasapp.player.NullPlaybackStateListener;
import com.apchernykh.rozhlasapp.services.BackgroundPlaybackService;
import com.apchernykh.rozhlasapp.services.IInetConnectivityInfo;
import com.apchernykh.rozhlasapp.services.NullInetConnectivityInfo;
import com.apchernykh.rozhlasapp.ui.IViewsExpirationListener;
import com.apchernykh.rozhlasapp.ui.NullViewsExpirationListener;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.rozhlasapp.ui.inetstate.IInetStateListener;
import com.apchernykh.rozhlasapp.ui.inetstate.NullInetStateListener;

import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;

public class ApplicationModel implements Parcelable {

	public static String TAG = "Debug1 ";
	public ArrayList<PodcastFeedInfo> mFeeds = new ArrayList<PodcastFeedInfo>();
	public IPlaybackService mBackgroundPlaybackService = new NullPlaybackService();
	public IInetConnectivityInfo mInetInfo = new NullInetConnectivityInfo();
	public final static long mStationsListExpirationTimeout = 12000000; // refresh stations list once
																		// per 3 hours
	public long mStationsListLasUpdated = 0;
	public IInetStateListener mOnInetStateListener = new NullInetStateListener();
	public IPlaybackStateListener mOnPlaybackStateListener  = new NullPlaybackStateListener();
	public IViewsExpirationListener mOnViewsExpiredListener = new NullViewsExpirationListener();
	public List<StationInfo> mStationsList = new ArrayList<StationInfo>();
	public int mStationSelected = 0;
	public static long mNextRefresh = 0;
		
	public enum EPlaybackStatuses {
		PLAYING, STOPPED, CONNECTION_IN_PROGRESS, PAUSED, PAUSED_WAIT_PHONE_HANG
	}

	public static final Parcelable.Creator<ApplicationModel> CREATOR = new Parcelable.Creator<ApplicationModel>() {
		public ApplicationModel createFromParcel(Parcel iParcel) {
			return new ApplicationModel(iParcel);
		}

		public ApplicationModel[] newArray(int iSize) {
			return new ApplicationModel[iSize];
		}
	};

	public ApplicationModel(Parcel iParcel) {
		mStationSelected = iParcel.readInt();
		mStationsList = iParcel.createTypedArrayList(StationInfo.CREATOR);
		if(mStationsList == null)
		{
			mStationsList = new ArrayList<StationInfo>();
		}
		mStationsListLasUpdated = iParcel.readLong();
		mNextRefresh = 0;
	}

	public ApplicationModel() {
	}

	@Override
	public void writeToParcel(Parcel oParcel, int iUnusedFlags) {
		oParcel.writeInt(mStationSelected);
		oParcel.writeTypedList(mStationsList);
		oParcel.writeLong(mStationsListLasUpdated);
	}

	public final OnClickListener onPlayButtonListener = new OnClickListener() {
		@Override
		public void onClick(View iView) {
			onStartPlayback(iView.getContext());
		}
	};

	public final OnClickListener onStopButtonListener = new OnClickListener() {
		@Override
		public void onClick(View iView) {
			RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.stopPlayback();
		}
	};

	public void onStartPlayback(Context iContext) {
		if (mStationSelected == -1) {
			throw new NullPointerException(
					"mStationSelected is unexpectedly -1");
		}
		if (mStationsList.size() > mStationSelected) {
			StationInfo aCurrentStation = mStationsList.get(mStationSelected);
			startPlayback(iContext, aCurrentStation);
		}
	}

	//TODO: find a way to get rid of these 2 methods startPlayback
	private void startPlayback(Context iContext, StationInfo iCurrentStation) {
		Intent aIntent = new Intent(iContext, BackgroundPlaybackService.class);
		iContext.startService(aIntent);
		mBackgroundPlaybackService.startPlayback(
				iCurrentStation);
	}

	public void startPlayback(Context iContext,
			DetailedUrl iDetailedUrl) {
		Intent aIntent = new Intent(iContext, BackgroundPlaybackService.class);
		iContext.startService(aIntent);
		mBackgroundPlaybackService.startPlayback(iDetailedUrl);
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	public static void notifyViewExpirationTime(long iExpirationTime) {
		// TODO all views who want to be refreshed, notify their expiration
		// timers.
		// Only the least will be saved, and upon next refresh, all the views
		// which are still alive,
		// will renotify
		// By the way, we don't book refreshes in the past, and in the nearest
		// future less than 5 seconds ahead

		long aCurrentTime = System.currentTimeMillis();
		if (mNextRefresh < aCurrentTime + 1000) {
			mNextRefresh = Long.MAX_VALUE;
		}

		if (iExpirationTime > aCurrentTime + 5000) {
			long aNextRefresh = Math.min(mNextRefresh, iExpirationTime);
			if (aNextRefresh != mNextRefresh) {
				RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
						.reScheduleViewExpiration(aNextRefresh);
			}
			mNextRefresh = aNextRefresh;
		}
	}

	public static void preferencesChanged() {
		RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.onPreferencesChanged();
	}

	public StationInfo getSelectedStationInfo() {
		if (mStationsList == null || mStationSelected < 0 || mStationSelected >= mStationsList.size()) {
			mStationSelected = 0;
		}
		if (mStationsList.size() > mStationSelected) {
			return mStationsList.get(mStationSelected);
		} else {
			return null;
		}
	}

	public long getExpirationTime()
	{
		return mStationsListLasUpdated+mStationsListExpirationTimeout;
	}
}
